<?php

namespace app\api\controller;

use think\Db;

class Shein
{
    public function index()
    {
        $orders = [];
        $SheinSDK = new SheinSDK();
        $SheinSDK->getSplitOrder();

        $SheinSDK->getOrders($orders);
    }

    public function sendOrders()
    {
        $res = Db::name('trade_shein')
            ->where('auto_claim_status',0)
            ->select();

        $orderInfo = [];
        foreach($res as $k => $v){
            $v['orderExtends'] = json_decode($v['orderExtends'],JSON_UNESCAPED_UNICODE);
            $goodsInfo = [];
            foreach($v['orderExtends'] as $k1 => $v1){
                if ($v1['orderQuantity'] == 1 ){
                    $goodsInfo = $v1;
                }
            }
            $orderInfo[] = [
                "order_sn"=> $v['orderNo'],
                "name"=> "王二",
                "mobile"=>"13211113995",
                "address"=> "开发区总部基地",
                "province"=> "江苏省",
                "city"=>"南京市",
                "area"=> "江宁区",
                "town"=>"庐阳街道",
                "post_code"=> "211100",
                "delivery"=>"仓库自提",
                "warehouse_name"=> "天马连云港仓",
                "pt_name"=> "shein",
                "goods_info"=> [
                    [
                        "order_sn_sub"=> "",
                        "goods_no"=> $goodsInfo['supplierCode'],
                        "amount"=> "1",
                        "size"=> substr($goodsInfo['suffixZh'], strripos($goodsInfo['suffixZh'], "R") + 1),
                        "barcode"=> $goodsInfo['skc']
                    ]
                ]
            ];
        }
        $params_pro = [
            'sign' => '12c3eebac58bd707df14f69473025416',
            'name' => 'Mr朱',
            'pwd' => '123456' ,
            'orders_info' => json_encode($orderInfo)
        ];
        $params_dev = [
            'sign' => 'e49e0a9bdfc795eb0f0330d51da36294',
            'name' => '花朵分销商B',
            'pwd' => '123456' ,
            'orders_info' => json_encode($orderInfo)
        ];
        $method = '/api/openapi/setOrderInfos.do';
        $url = 'http://open.xingyunyezi.com';
        $url = 'http://253.open.test.tmyd.tianmagroup.com';

        $res = post_params($url.$method,$params_dev);
        $res = json_decode($res,JSON_UNESCAPED_UNICODE);
        if (isset($res['error_code'])){
            return errorMsg($res['error_info']);
        }

        foreach($res as $k => $v){
            if (!isset($v['order_sn'])){
                return errorMsg($orderInfo[$k]['order_sn'].'下单失败');
            }
            if ($v['status'] == 0 ){
                DB::name('trade_shein')
                    ->where('orderNo',$v['order_sn'])
                    ->update(['auto_claim_status'=>1,'status'=>2,'price'=>$v['orders'][0]['price'],'trade_id'=>$v['trade_id'],'down_status'=> $v['status'],'orders'=>json_encode($v['orders']),'order_id'=>$v['orders'][0]['order_id']]);
            }else{
                DB::name('trade_shein')
                    ->where('orderNo',$v['order_sn'])
                    ->update(['auto_claim_status'=>1,'down_status'=>$v['status'],'remarks'=>$v['info']]);
            }
        }

        return success();



    }
}